package com.briup.myblog.web.controller;

import com.briup.myblog.bean.basic.Category;
import com.briup.myblog.bean.ext.CategoryExt;
import com.briup.myblog.service.ICategoryService;

import com.briup.myblog.utils.Result;
import com.briup.myblog.utils.ResultUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "资讯栏目")
@Service
@RestController
@RequestMapping("/auth/category")
public class CategoryController {
    @Autowired
    private ICategoryService categoryService;

    @ApiOperation(value = "批量删除栏目")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids",value = "栏目编号",required = true,allowMultiple = true)
    })
    @DeleteMapping("/batchDelete")
    public Result CategoryBatchDelete(Integer[] ids){
        categoryService.CategoryBatchDeleteByIds(ids);
        return ResultUtil.success("批量删除成功");
    }

    @ApiOperation(value = "通过id删除栏目")
    @ApiImplicitParam(name = "id",value = "栏目编号",required = true)
    @DeleteMapping("/deleteById")
    public Result CategoryDeleteById(Integer id){
        categoryService.categoryDeleteById(id);
        return ResultUtil.success("删除成功");
    }

    @ApiOperation(value = "查询所有栏目")
    @GetMapping("/find/all")
    public Result categoryAll(){
        List<CategoryExt> list = categoryService.findCategoryAll();
        return ResultUtil.success(list);
    }

    @ApiOperation(value = "分页查询所有资讯栏目信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "当前页码",required = true),
            @ApiImplicitParam(name = "pageSize",value = "每页大小",required = true),
    })
    @GetMapping("/page/queryAll")
    public Result categoryAllToPage(Integer pageNum,Integer pageSize){
        PageInfo<CategoryExt> pageInfo = categoryService.findCategoryAllToPage(pageNum,pageSize);
        return ResultUtil.success(pageInfo);
    }

    @ApiOperation(value = "保存资讯信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryDescription",value = "栏目描述信息",required = true),
            @ApiImplicitParam(name = "categoryName",value = "栏目名称",required = true),
            @ApiImplicitParam(name = "categoryOrder",value = "栏目排序",required = true),
            //栏目父级id（如果是二级栏目，需要添加父栏目id）
            @ApiImplicitParam(name = "categoryParentId",value = "栏目父级id"),
    })
    @PostMapping("/save")
    public Result saveCategory(String categoryDescription,String categoryName,Integer categoryOrder,Integer categoryParentId){
        categoryService.saveCategory(categoryDescription,categoryName,categoryOrder,categoryParentId);
        return ResultUtil.success("保存成功");
    }

    @ApiOperation(value = "更新栏目信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryId",value = "栏目id",required = true),
            @ApiImplicitParam(name = "categoryDescription",value = "栏目描述信息",required = true),
            @ApiImplicitParam(name = "categoryName",value = "栏目名称",required = true),
            @ApiImplicitParam(name = "categoryOrder",value = "栏目排序",required = true),
            //栏目父级id（如果是二级栏目，需要添加父栏目id）
            @ApiImplicitParam(name = "categoryParentId",value = "栏目父级id"),
    })
    @PutMapping("/update")
    public Result updateCategory(Category category){
        categoryService.updateCategory(category);
        return ResultUtil.success("更新栏目成功");
    }
}
